<?php
class JddjApi_model extends CI_Model {
    /*
        添加分类
        params
            store_id 店铺id
            category_id 分类ID
            category_name 分类名称
    */
    public function addCategory($store_id = 0, $category_id = 0, $category_name = '')
    {
        $this->load->library('AdminApi/JddjApi', '', 'JddjApi');
        $JddjApi = $this->JddjApi;
        $add = $JddjApi->addCategoryInfo($store_id, $category_id, $category_name);

        if($add['errNo'] == "0"){
            $res = array(
                "status" => "success"
            );
        }else{
            $res = array(
                "status" => "error",
                "info" => $add['data']['error']['message'] ? $add['data']['error']['message'] : $add['errMsg']
            );
        }

        return $res;
    }

    /*
        删除分类
        params
            store_id 店铺id
            other_category_id 第三方分类id
    */
    public function delCategory($store_id = 0, $other_category_id = '')
    {
        $this->load->library('AdminApi/JddjApi', '', 'JddjApi');
        $JddjApi = $this->JddjApi;

        $del = $JddjApi->delCategoryInfo($store_id, $other_category_id);

        if($del['errNo'] == "0"){
            $res = array(
                "status" => "success"
            );
        }else{
            $res = array(
                "status" => "error",
                "info" => $del['data']['error']['message'] ? $del['data']['error']['message'] : $del['errMsg']
            );
        }

        return $res;
    }

     /*
        更新分类
        params
            other_category_id 第三方分类id
            other_category_name 第三方分类名称
    */
    public function updateCategory($other_category_id = 0, $other_category_name = '')
    {
        $this->load->library('AdminApi/JddjApi', '', 'JddjApi');
        $JddjApi = $this->JddjApi;

        $update = $JddjApi->updateCategoryInfo($other_category_id, $other_category_name);

        if($update['errNo'] == "0"){
            $res = array(
                "status" => "success"
            );
        }else{
            $res = array(
                "status" => "error",
                "info" => $update['data']['error']['message'] ? $update['data']['error']['message'] : $update['errMsg']
            );
        }

        return $res;
    }

    /*
        初始化分类
        params
            store_id 店铺id
    */
    public function initCategory($store_id = 0)
    {
        $this->load->library('AdminApi/JddjApi', '', 'JddjApi');
        $JddjApi = $this->JddjApi;

        $del = $JddjApi->initCategoryInfo($store_id);

        if($del['errNo'] == "0"){
            $res = array(
                "status" => "success"
            );
        }else{
            $res = array(
                "status" => "error",
                "info" => $del['data']['error']['message'] ? $del['data']['error']['message'] : $del['errMsg']
            );
        }

        return $res;
    }

    /*
         同步商品
         params
            store_id 店铺id
            product_info[] 商品信息数组对象
    */
    public function syncProduct($store_id = 0, $product_info = array())
    {
        $queueKey = sprintf("KCS_QUEUE_%s_SYNC_PRODUCT", "JINGDONGDJ");

        $this->load->library("MyRedis", '', "MyRedis");
        $MyRedis = $this->MyRedis;

        $queueVal = "%s_%s_%s"; //store_id 、 category_id 、 product_id

        foreach ($product_info as $key => $value) {
            $category_id = $value['other_category_id'];
            $product_id = $value['ID'];
            $tmpValue = sprintf($queueVal, $store_id, $category_id, $product_id);

            $MyRedis->RPUSH($queueKey, $tmpValue);
        }

        $res = array(
            "status" => "success"
        );
        return $res;
    }

    /*
         同步商品
         params
            sync_stock_queue 商品同步队列数据对象
    */
    public function syncStock($sync_stock_queue = array())
    {
        $queueKey = sprintf("KCS_QUEUE_%s_SYNC_PRODUCT_STOCK", "JINGDONGDJ");

        $this->load->library("MyRedis", '', "MyRedis");
        $MyRedis = $this->MyRedis;

        $queueVal = "%s_%s_%s"; //store_id 、 other_product_id 、 product_stock

        foreach ($sync_stock_queue as $key => $value) {
            $store_id = $value['store_id'];
            $other_product_id = $value['other_product_id'];
            $product_stock = $value['product_stock'];
            
            $tmpValue = sprintf($queueVal, $store_id, $other_product_id, $product_stock);

            $MyRedis->RPUSH($queueKey, $tmpValue);
        }

        $res = array(
            "status" => "success"
        );
        return $res;
    }

    /*
         同步商品价格
         params
            sync_price_queue 商品同步队列数据对象
    */
    public function syncPrice($sync_price_queue = array())
    {
        $queueKey = sprintf("KCS_QUEUE_%s_SYNC_PRODUCT_PRICE", "JINGDONGDJ");

        $this->load->library("MyRedis", '', "MyRedis");
        $MyRedis = $this->MyRedis;

        $queueVal = "%s_%s_%s"; //store_id 、 other_product_id 、 product_price

        foreach ($sync_price_queue as $key => $value) {
            $store_id = $value['store_id'];
            $other_product_id = $value['other_product_id'];
            $product_price = $value['product_price'];
            
            $tmpValue = sprintf($queueVal, $store_id, $other_product_id, $product_price);

            $MyRedis->RPUSH($queueKey, $tmpValue);
        }

        $res = array(
            "status" => "success"
        );
        return $res;
    }
}